<!-- 价格分析 -->
<template>
  <div>
    <el-card style="width: 100%; margin: 0 auto">
      <div class="box">
        <div class="left">
          <div class="text">
            <div class="text_title">食材名称:</div>
            <div class="text_name">{{ foodData.name }}</div>
          </div>
          <div class="text">
            <div class="text_title">供应商:</div>
            <div class="text_name">{{ foodData.supplierName }}</div>
          </div>
          <div class="text">
            <div class="text_title">当前批发价:</div>
            <div class="text_name">{{ foodData.wholePrice }}</div>
          </div>
          <div class="text">
            <div class="text_title">当前零售价:</div>
            <div class="text_name">{{ foodData.sellPrice }}</div>
          </div>
          <div class="text">
            <div class="text_title">当前采购价:</div>
            <div class="text_name">{{ foodData.purchasePrice }}</div>
          </div>
        </div>
        <div class="rgt">价格更新于： {{ foodData.updateTime }}</div>
      </div>
    </el-card>
    <el-card style="width: 100%; margin: 0 auto; margin-top: 10px">
      <div class="rgt">价格趋势</div>
      <div class="icons" style="width: 100%; height: 400px">
        <div id="chart" ref="chart" style="width: 100%; height: 100%" />
      </div>
    </el-card>

    <el-card style="width: 100%; margin: 0 auto; margin-top: 10px">
      <div class="rgt">价格更新记录</div>
      <el-table :data="TableData" style="width: 100%; margin-top: 30px">
        <el-table-column prop="updateTime" label="价格更新日期" />
        <el-table-column prop="wholePrice" label="批发价" />
        <el-table-column prop="sellPrice" label="零售价" />
        <el-table-column prop="purchasePrice" label="采购价" />
        <el-table-column prop="addAccountName" label="更新人" />
        <el-table-column prop="date" label="操作">
          <template #default="scope">
            <el-button type="text" class="btn" size="small" @click="uploadEdit(scope.row)">
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-button class="back" @click="back">返回</el-button>

    <!-- 编辑 -->
    <priceDialog
      v-if="dialogVisible"
      :listData="listData"
      :priceId="priceId"
      @closePrice="handelClose"
    />
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { foodsGet, foodsPriceList } from "@/api/diet/food/food";
import { useRoute, useRouter } from "vue-router";
import { foodPriceAddParams, foodsListData, foodsPriceData } from "@/api/diet/food/type";
import * as echarts from "echarts";
import priceDialog from "@/components/diet/priceUpdate.vue";
import dayjs from "dayjs";
const route = useRoute();
const router = useRouter();

let id = 0;

// 图表
const chart = ref<HTMLElement>();
type EChartsOption = echarts.EChartsOption;
let myChart: echarts.ECharts | null = null;
let option: EChartsOption;
const getEchart = () => {
  if (!myChart) {
    myChart = echarts.init(chart.value);
  }
  option = {
    xAxis: {
      type: "category",
      data: data.value,
    },
    yAxis: {
      type: "value",
    },
    series: [
      {
        data: wholePrice.value,
        type: "line",
        smooth: true,
      },
      {
        data: sellPrice.value,
        type: "line",
        smooth: true,
      },
      {
        data: purchasePrice.value,
        type: "line",
        smooth: true,
      },
    ],
  };
  myChart.setOption(option);
};

// 根据食材id获取食材信息
let foodData = ref<foodsListData>({
  id: 0,
  companyId: 0,
  name: "",
  unit: "",
  supplierId: 0,
  wholePrice: "",
  sellPrice: "",
  purchasePrice: "",
  updateTime: "",
  supplierName: "",
});
const getFoodData = async () => {
  const res = await foodsGet(id);
  // console.log(res);
  foodData.value = res;
};

// 根据食材id获取价格列表
let data = ref<string[]>([]); // 更新的日期
let wholePrice = ref<string[]>([]); //批发价
let sellPrice = ref<string[]>([]); //零售价
let purchasePrice = ref<string[]>([]); //采购价
const TableData = ref<foodsPriceData[]>([]);
const getPriceList = async () => {
  const res = await foodsPriceList(id);
  TableData.value = res.list;
  data.value = res.list.map((item) => dayjs(item.updateTime).format("MM-DD"));
  wholePrice.value = res.list.map((item) => item.wholePrice);
  sellPrice.value = res.list.map((item) => item.sellPrice);
  purchasePrice.value = res.list.map((item) => item.purchasePrice);
  await nextTick(); // 等待 DOM 更新
  getEchart();
};

// 编辑
let priceId = ref(0);
const dialogVisible = ref(false);
let listData = reactive<foodPriceAddParams>({
  id: 0,
  foodId: 0,
  wholePrice: "",
  sellPrice: "",
  purchasePrice: "",
  updateTime: "",
});
const uploadEdit = (row: foodsPriceData) => {
  priceId.value = row.foodId;
  listData = row;
  dialogVisible.value = true;
};

// 关闭弹框
const handelClose = () => {
  dialogVisible.value = false; // 价格更新
  getPriceList();
};

// 返回
const back = () => {
  router.push("/diet/food");
};

onMounted(() => {
  id = route.query.id as unknown as number;
  getFoodData();
  getPriceList();
});
</script>

<style scoped lang="less">
.box {
  display: flex;
  justify-content: space-between;

  .text {
    display: flex;
    font-size: 14px;
    margin-bottom: 5px;

    .text_title {
      width: 130px;
    }
  }

  .rgt {
    font-size: 14px;
  }
}

.btn {
  color: rgb(64, 158, 255);
}

.back {
  display: flex;
  justify-content: center;
  margin: 30px auto;
}
</style>
